package com.gome.ss.schedule.model;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.gome.ss.core.download.StartRequest;
import com.gome.ss.core.plugin.Plugin;
import com.gome.ss.core.plugin.SpiderLocalPluginHandle;
import com.gome.ss.core.schedule.ScheduleCore;
import com.gome.ss.core.schedule.ScheduleManager;
import com.gome.ss.core.schedule.TriggerConfig;
import com.gome.ss.core.schedule.TriggerCron;
import com.gome.ss.core.schedule.TriggerStart;
import com.mongodb.DBObject;

public class PluginModel {
	
	/*
	 *   obj contains plugin's :
	 *   name
	 *   desc
	 *   fileName
	 *   qualified
	 *   processable
	 *   // startRequests
	 *   [{
	 *   	groupName
	 *   	retryCount
	 *   	link
	 *   }, ...]
	 *   isPrcessing
	 *   isLaunched
	 *   isTriggerCron
	 *   isTriggerNormal
	 *   triggerCron
	 *   triggerTime
	 *   scheduleInterval
	 */
	JSONObject obj = new JSONObject();

	public PluginModel(Plugin plugin) {
		obj.put("name", plugin.getHandle().getPluginName());
		obj.put("desc", plugin.getHandle().getPluginDesc());
		obj.put("startMethod", plugin.getHandle().getStartRequestMethod());
		obj.put("fileName", plugin.getHandle().getPluginFileName());
		obj.put("threadCount", plugin.getMaxThreadCount());
		
		ScheduleCore sc = ScheduleManager.getInstance().getScheduleCore(plugin.getHandle().getPluginQualifiedName());
		if (sc != null) {
			obj.put("scheduleInterval", sc.getScheduleInterval());
		}
		
		if (plugin.isLocalPlugin()) {
			SpiderLocalPluginHandle handle = (SpiderLocalPluginHandle) plugin.getHandle();
			obj.put("qualified", handle.getConfig().getQualifiedName());
		} else {
			obj.put("qualified", plugin.getHandle().getPluginQualified());
		}
		
		obj.put("processable", plugin.getHandle().processable());
		
		JSONArray startRequests = new JSONArray();
		for (StartRequest req : plugin.getHandle().getStartReqs()) {
			JSONObject robj = new JSONObject();
			robj.put("groupName", req.groupName);
			robj.put("link", req.link);
			startRequests.put(robj);
		}
		obj.put("startRequests", startRequests);
		
		if (plugin.getHandle().processable()) {
			obj.put("isProcessing", plugin.getTaskProcess().checkProcessing());
			obj.put("isLaunched", plugin.getTaskProcess().isLaunched());
			
			if (plugin.getTaskProcess().isLaunched()) {
				TriggerConfig trigger = plugin.getTaskProcess().getTrigger();
				obj.put("triggerType", trigger.getTriggerType());
				if (trigger.isCronType()) {
					TriggerCron tc = (TriggerCron)trigger;
					obj.put("triggerCron", tc.cron());
				} else if (trigger.isNormalType() || trigger.isCycleType()) {
					TriggerStart ts = (TriggerStart)trigger;
					obj.put("triggerTime", ts.time());
				}
			}
			
			obj.put("taskID", plugin.getTaskProcess().getTaskID());
			obj.put("reference", ScheduleManager.getInstance().getScheduleCore(plugin.getHandle().getPluginQualifiedName()).taskReference(plugin.getTaskProcess().getTaskID()));
			obj.put("runningReference", ScheduleManager.getInstance().getScheduleCore(plugin.getHandle().getPluginQualifiedName()).taskReferenceInThreadPool(plugin.getTaskProcess().getTaskID()));
			
			JSONArray historyTask = new JSONArray();
			List<DBObject> historys = plugin.getTaskProcess().getHistoryTaskInfo();
			for ( DBObject history : historys) {
				JSONObject hisobj = new JSONObject();
				hisobj.put("totalRef", history.get("totalRef"));
				hisobj.put("startDate", history.get("startDate"));
				hisobj.put("stopDate", history.get("stopDate"));
				hisobj.put("taskID", history.get("taskID"));
				hisobj.put("stopStatus", history.get("stopStatus"));
				historyTask.put(hisobj);
			}
			
			obj.put("historyTask", historyTask);
		}
	}
	
	public String toString() {
		return obj.toString();
	}
	
	public JSONObject getJSONObject() {
		return obj;
	}
}
